package com.mtw.bbs.common.web.util;

import com.alibaba.nacos.common.codec.Base64;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

/**
 * aes工具
 */
@Slf4j
public class AccessLimitAESUtil {

    /**
     * 算法名称/加密模式/数据填充方式
     */
    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";

    /**
     * AES加解密密钥
     */
    private static final String KEY = "0x186a0000000000";



    /**
     * AES解密方法
     */
    public static  String decrypt(String decryptStr) {

        if (StringUtils.isBlank(decryptStr)){
            return null;
        }
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY.getBytes(), "AES"));
            byte[] encryptBytes = Base64.decodeBase64(decryptStr.getBytes());
            byte[] decryptBytes = cipher.doFinal(encryptBytes);
            return new String(decryptBytes);
        }catch (Exception e){
            log.error("decrypt：{} ，解密异常", decryptStr, e);
        }

        return null;
    }
}
